package strings;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author LiangMu
 * @version \$Id: P2.java, v 0.1, 2023年01月09日 13:47 LiangMu Exp $$
 */
public class P2 {
    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();
        int len = chars.length;
        if (len <= 1) {
            return s;
        }
        int start, end, index = 0, curSize = 1, ansSize = 1, ansStart = 0, ansEnd = 0;
        // 奇数判断
        while (index < len) {
            start = index - 1;
            end = index + 1;
            if (start < 0 || end >= len) {
                ++index;
            } else {
                while (start >= 0 && end < len) {
                    char c1 = chars[start];
                    char c2 = chars[end];
                    if (c1 == c2) {
                        curSize = end - start + 1;
                        ansSize = Integer.max(curSize, ansSize);
                        end++;
                        start--;
                    } else {
                        int cur = end - start - 1;
                        int ans = ansEnd - ansStart + 1;
                        if (cur > ans) {
                            ansStart = start + 1;
                            ansEnd = end - 1;
                        }
                        break;
                    }
                }
            }
        }
        return s.substring(ansStart, ansEnd);
    }
}
